<?php
/**
 * @version		$Id: restore.php 10094 2008-03-02 04:35:10Z yopensource $
 * @package		YOS
 * @subpackage	YOS News
 * @copyright	Copyright (C) 2005 - 2008 YOS. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );
jimport('joomla.filesystem.archive');
jimport( 'joomla.installer.installer' );
jimport('joomla.installer.helper');

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

/**
 * Extension Manager Install Model
 *
 * @package		Joomla
 * @subpackage	Installer
 * @since		1.5
 */
class YOS_NewsModelRestore extends JModel
{
	/** @var object JTable object */
	var $_table = null;

	/** @var object JTable object */
	var $_url = null;

	/**
	 * Overridden constructor
	 * @access	protected
	 */
	function __construct()
	{
		parent::__construct();

	}

	function install()
	{
		global $mainframe;

		$this->setState('action', 'install');		

		switch(JRequest::getWord('installtype'))
		{
			case 'upload':
				$package = $this->_getPackageFromUpload();
				break;

			default:
				$this->setState('message', 'No Install Type Found');
				return false;
				break;
		}
		// Was the package unpacked?
		if (!$package) {
			$this->setState('message', 'Unable to find install package');
					
			return false;
		}
		// Get a database connector
		//$db = & JFactory::getDBO();

		// Get an installer instance
		$installer =& JInstaller::getInstance();
	
		// Package installed sucessfully
		$msg = JText::sprintf(JText::_('Success'));
		$result = true;		

		// Set some model state values
		$mainframe->enqueueMessage($msg);
		$this->setState('result', $result);

		// Cleanup the install files
		if (!is_file($package['packagefile'])) {
			$config =& JFactory::getConfig();
			$package['packagefile'] = $config->getValue('config.tmp_path').DS.$package['packagefile'];
		}

		JInstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']);

		return $result;
	}

	/**
	 * @param string The class name for the installer
	 */
	function _getPackageFromUpload()
	{
		// Get the uploaded file information
		$userfile = JRequest::getVar('install_package', null, 'files', 'array' );

		// Make sure that file uploads are enabled in php
		if (!(bool) ini_get('file_uploads')) {
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLFILE'));
			return false;
		}
		// Make sure that zlib is loaded so that the package can be unpacked
		if (!extension_loaded('zlib')) {
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLZLIB'));
			return false;
		}
		// If there is no uploaded file, we have a problem...
		if (!is_array($userfile) ) {
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('No file selected'));
			return false;
		}
		// Check if there was a problem uploading the file.
		if ( $userfile['error'] || $userfile['size'] < 1 )
		{
			JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLUPLOADERROR'));
			return false;
		}
		// Build the appropriate paths
		$config =& JFactory::getConfig();
		$tmp_dest 	= $config->getValue('config.tmp_path').DS.$userfile['name'];
		$tmp_src	= $userfile['tmp_name'];

		// Move uploaded file
		jimport('joomla.filesystem.file');
		$uploaded = JFile::upload($tmp_src, $tmp_dest);
		// Path to the archive
		$archivename = $tmp_dest;

		// Temporary folder to extract the archive into
		$tmpdir = uniqid('yos_news_');

		// Clean the paths to use for archive extraction
		$extractdir = JPath::clean(dirname($tmp_dest).DS.$tmpdir);
		$archivename = JPath::clean($archivename);
		
		$package['packagefile']=$archivename;
		$package['extractdir']=$extractdir;

		// do the unpacking of the archive
		if (JFile::getExt($archivename)=='xml') {
			JFolder::create($extractdir);
			JFile::copy($archivename,$extractdir.DS.JFile::getName($archivename));
		} else {
			$result = JArchive::extract( $archivename, $extractdir);
		}
		
		// Search the install dir for an xml file
		$files = JFolder::files($extractdir, '\.xml', true, true);
		foreach ($files as $file) {
			$xml = & JFactory::getXMLParser('Simple');
			if ($xml->loadFile($file))
			{
				if(!$tablename	=	$xml->document->attributes('table')){
					JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLFILE'));
					return false;
				}
				
				if ($tablename!='#__yos_news_crawler') {
					JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLFILE'));
					return false;
				}
				$row 	=& JTable::getInstance('YOS_News_Crawler','Table');
				
				$row->link				=	$xml->document->link[0]->data();
				$row->url				=	$xml->document->url[0]->data();				
				$row->category			=	$xml->document->category[0]->data();
				$row->title				=	$xml->document->title[0]->data();
				$row->description		=	$xml->document->description[0]->data();
				$row->getlink_reg 		=	$xml->document->getlink_reg[0]->data();
				$row->getlink_sub 		=	$xml->document->getlink_sub[0]->data();
				$row->find_replace_links=	$xml->document->find_replace_links[0]->data();
				$row->gettitle_reg 		=	$xml->document->gettitle_reg[0]->data();
				$row->gettitle_sub 		=	$xml->document->gettitle_sub[0]->data();
				$row->find_replace_titles=	$xml->document->find_replace_titles[0]->data();
				$row->dataway 			=	$xml->document->dataway[0]->data();
				$row->getintro_reg 		=	$xml->document->getintro_reg[0]->data();
				$row->getintro_sub 		=	$xml->document->getintro_sub[0]->data();
				$row->cut_content 		= 	$xml->document->cut_content[0]->data();
				$row->numberword 		=	$xml->document->numberword[0]->data();
				$row->find_replace_intro=	$xml->document->find_replace_intro[0]->data();
				$row->getfull_reg 		=	$xml->document->getfull_reg[0]->data();
				$row->getfull_sub 		=	$xml->document->getfull_sub[0]->data();
				$row->find_replace_full =	$xml->document->find_replace_full[0]->data();
				$row->has_multiple_pages=	$xml->document->has_multiple_pages[0]->data();
				$row->multiple_way 		=	$xml->document->multiple_way[0]->data();
				$row->pages_detect_reg 	=	$xml->document->pages_detect_reg[0]->data();
				$row->pages_detect_sub 	=	$xml->document->pages_detect_sub[0]->data();
				$row->multipage_title_reg=	$xml->document->multipage_title_reg[0]->data();
				$row->multipage_title_sub=	$xml->document->multipage_title_sub[0]->data();
				$row->multipage_reg		=	$xml->document->multipage_reg[0]->data();
				$row->multipage_sub		=	$xml->document->multipage_sub[0]->data();
				$row->find_replace_multipage=	$xml->document->find_replace_multipage[0]->data();
				$row->catname			=	$xml->document->catname[0]->data();
				$row->numbercontent		=	$xml->document->numbercontent[0]->data();
					$row->pid 			=	$xml->document->pid[0]->data();
				if ($row->pid!='(YOS)no plugin') {
					$row->pid 				=	$this->element2id($row->pid);
				}
				else {
					$row->pid=0;
				}				
				if (!$row->store()) {
					JError::raiseError(500, $row->getError() );
				}
			}
		}		
		$package['row']=&$row;		
		return $package;
	}
		// FUNCTION CONVERT FOR PLUGIN
	function element2id($elementName)
	{
		$db=JFactory::getDBO();
		$query='SELECT id 
				FROM #__plugins 
				WHERE folder='.$db->quote('yrm').'
				and element='.$db->quote($elementName);
		$db->setQuery($query);		
		$id=$db->loadResult();		
		return $id;
	}
}